package com.rpp.servlet;

import com.rpp.core.Mapper;

import java.io.InputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private int port;
    private Mapper mapper;

//    private Map<String, HttpServlet> servletMap;
//
//    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
//        this.socket = socket;
//        this.servletMap = servletMap;
//    }

    public RequestProcessor(Socket socket, int port, Mapper mapper) {
        this.socket = socket;
        this.port = port;
        this.mapper = mapper;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            // 资源处理
            //获取hostName 查找Host
            String hostName = socket.getInetAddress().getHostName();
            //根据hostName 查找Host
            Mapper.MappedHost host = mapper.findHost(hostName);

            //http://localhost:8080/demo1/rpp1
            // demo1/rpp1
            String url = request.getUrl();

            //优先处理自定义context,匹配不到就处理默认的context,也就是当前项目中的默认servlet
            List<Mapper.MappedContext> contexts = host.getContexts();
            for (Mapper.MappedContext context : contexts) {

                // demo1
                String path = context.getPath();
                System.out.println("请求路径：" + hostName + ":" + port + "/" + path);

                // url=/demo1/rpp1   path=/demo1
                if (url.startsWith(path)) {
                    Mapper.MappedWrapper wrapper = context.getWrapper();
                    Map<String, HttpServlet> servletMap = wrapper.getServletMap();
                    if (servletMap.containsKey(url)) {
                        // 动态资源servlet请求
                        HttpServlet httpServlet = servletMap.get(url);
                        httpServlet.service(request, response);
                    } else {
                        //处理静态资源  /demo1/index.html，需要获取自定义资源根路径，然后拼接url
                        //   docBase=/Users/webApps
                        String docBase = context.getDocBase();
                        //处理自定义项目静态资源
                        response.outputCustomHtml(docBase, request.getUrl());
                    }
                }
            }
            //匹配默认context
            Mapper.MappedContext defaultContext = host.getDefaultContext();
            Map<String, HttpServlet> servletMap = defaultContext.getWrapper().getServletMap();
            if (servletMap.containsKey(url)) {
                // 动态资源servlet请求
                HttpServlet httpServlet = servletMap.get(url);
                httpServlet.service(request, response);
            } else {
                //处理静态资源
                response.outputHtml(request.getUrl());
            }

            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
